#include "hexBinConverter.h"

std::string HexConverter( std::string hexNumber )
{
	std::string binaryString;
	for( unsigned int i = 0; i < hexNumber.size(); i++ )
	{
		switch( hexNumber[i] )
		{
		case '0':
			binaryString.append("0000");
			break;
		case '1':
			binaryString.append("0001");
			break;
		case '2':
			binaryString.append("0010");
			break;
		case '3':
			binaryString.append("0011");
			break;
		case '4':
			binaryString.append("0100");
			break;
		case '5':
			binaryString.append("0101");
			break;
		case '6':
			binaryString.append("0110");
			break;
		case '7':
			binaryString.append("0111");
			break;
		case '8':
			binaryString.append("1000");
			break;
		case '9':
			binaryString.append("1001");
			break;
		case 'A':
			binaryString.append("1010");
			break;
		case 'B':
			binaryString.append("1011");
			break;
		case 'C':
			binaryString.append("1100");
			break;
		case 'D':
			binaryString.append("1101");
			break;
		case 'E':
			binaryString.append("1110");
			break;
		case 'F':
			binaryString.append("1111");
			break;
		}
	}
	return binaryString;
}

std::string BinaryConverter( std::string binaryNumber )
{
	std::string hexString;
	if( binaryNumber.size() % 4 != 0 )
	{
		std::cout << "Error binary Number is not a multiple of 4" << std::endl;
		return "";
	}



	//divided the size of the binaryNumber by 4 because 4 bits
	//fit into one hex number.
	for( unsigned int i = 0; i < binaryNumber.size() / 4; i++ )
	{
		std::string binaryChunk;
		for( unsigned int j = i * 4; j < ( ( i * 4 ) + 4 ) ; j++ )
		{
			binaryChunk.push_back( binaryNumber[j] );
		}
		int test = atoi( binaryChunk.c_str() );
		switch( test )
		{
		case 0:
			hexString.push_back( '0' );
			break;
		case 1:
			hexString.push_back( '1' );
			break;
		case 10:
			hexString.push_back( '2' );
			break;
		case 11:
			hexString.push_back( '3' );
			break;
		case 100:
			hexString.push_back( '4' );
			break;
		case 101:
			hexString.push_back( '5' );
			break;
		case 110:
			hexString.push_back( '6' );
			break;
		case 111:
			hexString.push_back( '7' );
			break;
		case 1000:
			hexString.push_back( '8' );
			break;
		case 1001:
			hexString.push_back( '9' );
			break;
		case 1010:
			hexString.push_back( 'A' );
			break;
		case 1011:
			hexString.push_back( 'B' );
			break;
		case 1100:
			hexString.push_back( 'C' );
			break;
		case 1101:
			hexString.push_back( 'D' );
			break;
		case 1110:
			hexString.push_back( 'E' );
			break;
		case 1111:
			hexString.push_back( 'F' );
			break;
		}
	}
	return hexString;
	
}
